package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.mail.MessagingException;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.debug.ServletDebugInfo;
import com.conversationboard.email.Mail;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.Role;
import com.conversationboard.model.User;
import com.conversationboard.security.EncryptedKey;
import com.conversationboard.security.EncryptionException;

@WebServlet(name = "NewSiteAdminControllerServlet", urlPatterns = "/Admin/NewSiteAdminControllerServlet")
public class NewSiteAdminControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -174468632031267478L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String existingSiteAdminId = request.getUserPrincipal().getName();
		String toBeMadeSiteAdminId = request.getParameter("userid");
		String message = "";

		try {

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");

			User existingSiteAdmin = User.get(existingSiteAdminId);
			User toBeMadeSiteAdmin = User.get(toBeMadeSiteAdminId);

			/* Is user authorised? */

			String key = request.getParameter("key");
			EncryptedKey encryptedKey = new EncryptedKey();

			if (!encryptedKey.isValid(key, existingSiteAdminId)) {
				Logger.log(new Date(), "Part 1 of error: " + this.getClass().getName(), existingSiteAdmin);
				String postRequest = ServletDebugInfo.getRawRequest(request);
				Logger.log(new Date(), "Part 2 of error:\n\n" + postRequest, existingSiteAdmin);
				return;
			}

			if (existingSiteAdmin.getRole() == Role.SiteAdmin) {

				boolean success = toBeMadeSiteAdmin.makeSiteAdmin();

				if (success) {
					message = "User: " + toBeMadeSiteAdmin.getDisplayName() + " has been made a site admin.";
					request.setAttribute("message", message);
					dispatcher.forward(request, response);
				} else {
					message = "User has not been made a site admin; as this user is not registered";
					request.setAttribute("message", message);
					dispatcher.forward(request, response);
				}

				Mail mail = new Mail();
				mail.sendToSiteAdmins("New Site Administrator", "User: " + toBeMadeSiteAdmin.getDisplayName() + " has been made a site admin.");

			} else {
				request.setAttribute("message", "You do not have the authority to perform this operation.");
				dispatcher.forward(request, response);
			}

		} catch (SQLException e) {
			throw new ServletException(e);
		} catch (MessagingException e) {
			throw new ServletException(e);
		} catch (NamingException e) {
			throw new ServletException(e);
		} catch (EncryptionException e) {
			throw new ServletException(e);
		}

	}

}
